package com.frogs.ucenter.v1.request;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.exception.RequestIllegalException;

/**
 * 用户名验证请求参数类
 *
 * @author lijian
 * @version 2014年3月26日 - 下午7:25:06
 */
public class UserVerifyNameRequest extends Request {

	/**
	 * 用户名
	 */
	private String accountName;
	
	/** */
	public UserVerifyNameRequest() {
	}
	
	@Override
	public void parse(HttpServletRequest request) {
		
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		
		this.accountName = requestInfo.getParameter(ApiUrlParamEnum.accountName.getParamName(), "");
		if (StringUtil.isEmpty(accountName)) {
			throw new RequestIllegalException("用户名不能为空");
		}
		if (!StringUtil.isEmpty(accountName)) {
			if (accountName.length() < 2 || accountName.length() > 20) {
				throw new RequestIllegalException("用户名的长度在2-20位");
			}
			if (!Pattern.matches("^[a-zA-Z0-9_\u4e00-\u9fa5]+$", accountName)) {
				throw new RequestIllegalException("用户名只能是中文，字母，数字，下划线组成");
			} 
		}
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

}
